package com.stylefeng.guns.modular.system.task;

import java.net.URI;
import java.net.URL;

import org.apache.commons.lang.StringUtils;
import org.apache.http.client.methods.HttpGet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.core.util.HttpClientUtil;
import com.stylefeng.guns.modular.system.constants.ImportContants;
import com.stylefeng.guns.modular.system.model.Gdprice;
import com.stylefeng.guns.modular.system.model.Gpdata;
import com.stylefeng.guns.modular.system.service.IGdpriceService;
import com.stylefeng.guns.modular.system.service.IGpdataService;

@Service
public class ZjGzImportTask {

	private Logger log = LoggerFactory.getLogger(ZjGzImportTask.class);

	private static final HttpClientUtil clientUtil = new HttpClientUtil();
	
	@Autowired
	private IGdpriceService gdpriceService;
	
	@Autowired
    private IGpdataService gpdataService;
	
	public void execute(boolean isHoliday) {
		if(isHoliday){
			String url = ImportContants.getZlzjDataPathStr(1);
			HttpGet httpGet = HttpClientUtil.getGetRequest();
			try {
				URL http = new URL(url);
				URI uri = new URI(http.getProtocol(), http.getHost(), http.getPath(), http.getQuery(), null);
				httpGet.setURI(uri);
				String json = clientUtil.executeAndGetWithFailedRetry(httpGet, 6,10001);
				JSONObject jsonObj = JSON.parseObject(json);
				if(jsonObj.getIntValue("pages") > 0){
					parseDjgData(jsonObj.getString("data"));
					int pages = jsonObj.getIntValue("pages");
					for(int i= 2; i <= pages ; i++){
						url = ImportContants.getZlzjDataPathStr(i);
						http = new URL(url);
						uri = new URI(http.getProtocol(), http.getHost(), http.getPath(), http.getQuery(), null);
						httpGet.setURI(uri);
						json = clientUtil.executeAndGetWithFailedRetry(httpGet, 6,10001);
						jsonObj = JSON.parseObject(json);
						parseDjgData(jsonObj.getString("data"));
					}
				}
			} catch (Exception e) {
				log.error("get zjgz data is error!" + e);
			}finally{
				httpGet.abort();
			}
		}
	}
	private void parseDjgData(String data) {
		String[] djgs = StringUtils.substringsBetween(data, "\"", "\"");
		for(String djg : djgs){
			try{
				if(StringUtils.isNotEmpty(djg)){
					String[] detail = StringUtils.split(djg,",");
					String scode = detail[1];
					String rank = detail[5];
					String owerBk = detail[13];
					String date = detail[15];
					
					Gdprice gdprice = new Gdprice();
					gdprice.setgDm(scode);
					gdprice.setgTime(DateUtil.parse(date, "yyyy-MM-dd hh:mm:ss"));
					gdprice = gdpriceService.getGdpriceByDate(gdprice);
					if(gdprice != null){
						gdprice.setgZjgzRank(Integer.valueOf(rank));
						gdpriceService.updateById(gdprice);
					}
					
					Wrapper<Gpdata> wrapper = new EntityWrapper<Gpdata>(); 
					wrapper.eq("g_dm", scode);
					Gpdata gpdata = gpdataService.selectOne(wrapper);
					if(gpdata != null){
						if(StringUtils.isEmpty(gpdata.getgOwerBk())){
							gpdata.setgOwerBk(owerBk);
							gpdataService.updateById(gpdata);
						}
					}
				}
			}catch(Exception e){
				log.error("parse error!" ,e);
			}
		}
	}
}
